#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2025/1/20 13:41
# @Author : ZhengQL

import pytest
from pages.login_page import LoginPage


@pytest.fixture(scope='session', autouse=True)
def login_first(browser, pytestconfig):
    # 全局先登录
    context = browser.new_context()
    page = context.new_page()
    login = LoginPage(page)
    login.navigate()  # 如果是相对地址，需要传base_url到context
    login.login(username='Xz',password='zzz123')
    # 等待到加载到指定页面,否则无法保存成功
    page.wait_for_url(url='**/index.html')
    # 保存登陆的cookies
    print(f"项目的根目录：{pytestconfig.rootpath}")
    storage_path = pytestconfig.rootpath.joinpath('auth/login.json')
    context.storage_state(path=storage_path)
    # 关闭
    context.close()


# 覆盖原始的browser_context_args方法
@pytest.fixture(scope='session')
def browser_context_args(browser_context_args, playwright, pytestconfig):
    """
    添加context上下文传参，默认每个页面加载cookies
    :param browser_context_args:
    :param playwright:
    :param pytestconfig:
    :return:
    """
    return {
        "storage_state": pytestconfig.rootpath.joinpath('auth/login.json'),
        **browser_context_args,
    }